package so.sao.shop.supplier.web.app;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import so.sao.shop.supplier.domain.AppAddress;
import so.sao.shop.supplier.pojo.Result;
import so.sao.shop.supplier.pojo.input.AppDeliveryAddressInput;
import so.sao.shop.supplier.service.RegionService;
import so.sao.shop.supplier.service.app.AppDeliveryAddressService;

import javax.validation.Valid;
import java.util.List;

/**
 * 
 * 前端控制器
 * 
 *
 * @author zhangruibing
 * @since 2017-07-17
 */
@RestController
@RequestMapping("/deliveryAddresses")
public class AppDeliveryAddressController {

    @Autowired
    private AppDeliveryAddressService appDeliveryAddressService;

    @Autowired
    private RegionService regionService;

    /**
     * 新增收货地址
     *
     * @param appDeliveryAddressInput 收货地址入参
     * @return
     */
    @PostMapping("/save")
    public Result create(@Valid @RequestBody AppDeliveryAddressInput appDeliveryAddressInput) {
        return appDeliveryAddressService.saveDeliveryAddress(appDeliveryAddressInput);
    }

    /**
     * 编辑收货地址
     *
     * @param appDeliveryAddressInput 收货地址入参
     * @return
     */
    @PostMapping(value = "/update")
    public Result update(@Valid @RequestBody AppDeliveryAddressInput appDeliveryAddressInput) {
        return appDeliveryAddressService.updateDeliveryAddress(appDeliveryAddressInput);
    }

    /**
     * 批量删除收货地址
     *
     * @param addrIds 收货地址id集合
     */
    @DeleteMapping(value = "/bulk/delete")
    public Result delete(@RequestParam String addrIds, @RequestParam String userId) {
        return appDeliveryAddressService.deleteDeliveryAddress(addrIds, userId);
    }


    /**
     * 根据用户id查询收货地址列表
     *
     * @param userId 用户Id
     * @return
     */
    @GetMapping(value = "/getDeliveryAddressList")
    public Result findByUserId(@RequestParam String userId) {
        return appDeliveryAddressService.findByUserId(userId);
    }

    /**
     * 更改默认的收货地址
     *
     * @param addrId 收货地址id
     * @param userId 用户Id
     * @return
     */
    @PostMapping(value = "/setDefaultDeliveryAddr")
    public Result updateStatus(@RequestParam String addrId, @RequestParam String userId) {
        return appDeliveryAddressService.updateDeliveryAddressStatus(addrId, userId);
    }


    /**
     * 获取所有省市区数据
     *
     * @return
     */
    @GetMapping(value = "/getAddress")
    public List<AppAddress> getAddress(){
        return appDeliveryAddressService.getAllRegion();
    }

    /**
     * 获取默认收货地址
     * @return
     */
    @GetMapping(value = "/getDefaultDeliveryAddr")
    public Result getDefaultDeliveryAddrByUserId(@RequestParam String userId){
        return appDeliveryAddressService.findDefaultDeliveryAddrByUserId(userId);
    }
}